<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">码生成</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <el-col class="mr20 width100">
            <el-button
                class="btn-success width100"
                type="primary"
                @click="getSubCode"
            >生成码</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="mt30">
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品:" prop="electronicAntiProductId">
              <el-input v-model="productName " placeholder="请选择产品" @focus="productShow" clearable @clear="productClear" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="生产日期:" prop="productionDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.productionDate" suffix-icon="el-icon-date" type="date" placeholder="请选择生产日期" style="width:100%;" @change="onChangeProductionDate">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="失效日期:" prop="expirationDate">
              <el-date-picker value-format="yyyy-MM-dd" v-model="form.expirationDate" suffix-icon="el-icon-date" type="date" placeholder="请选择失效日期" style="width:100%;" @change="onChangeExpirationDate">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="公司:" prop="electronicAntiCompanyConfigurationId">
              <el-input v-model="name " placeholder="请选择公司" @focus="companyShow" clearable @clear="companyClear" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="有无隐藏码:" prop="generateCryptogram">
              <el-select v-model="form.generateCryptogram" placeholder="请选择" style="width:100%;">
                <el-option label="有" :value="true">有</el-option>
                <el-option label="无" :value="false">无</el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
             <el-form-item label="生成码数量:" prop="num">
              <el-input v-model.number="form.num" placeholder="请输入生成码数量" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 弹出数据 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" top="50px" append-to-body center :before-close="handleClose">
      <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
        <template v-if="type == '1'">
          <el-form-item label="产品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称检索"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </template>
        <template v-if="type == '2'">
          <el-form-item label="客户名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入客户名称检索"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </template>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="dataList" @row-click="onSelectData" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-if="type == '1'">
          <el-table-column label="产品编码" align="center" prop="code" />
          <el-table-column label="产品名称" align="center" prop="productName" />
          <el-table-column label="产品规格" align="center" prop="specifications" />
        </template>
        <template v-if="type == '2'">
          <el-table-column label="客户名称" align="center" prop="name" />
          <el-table-column label="生产方" align="center" prop="manufacturer" />
          <el-table-column label="委托方" align="center" prop="client" />
        </template>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getData"
      />
    </el-dialog>
  </div>
</template>

<script>
import { antiCodeGenerateCode, antiCodeList, antiCodeCompanyConfigList } from  "@/api/system/antiCode/antiCode"
export default {
  name: "index",
  data() {
    var checkAge = (rule, value, callback) => {
      if(value == 0){
        return callback(new Error('生成码数量必须大于0'))
      }
      if (!Number.isInteger(value)) {
        callback(new Error('请输入数字值'))
      } else {
        callback()
      }
    }
    return {
      productName: '',
      name: '',
      form: {
        electronicAntiProductId: '',
        electronicAntiCompanyConfigurationId: '',
        generateCryptogram: true,
        num: '',
        productionDate: '', // 生产日期
        expirationDate: '', // 失效日期
      },
      // 表单校验
      rules: {
        electronicAntiProductId: [
          { required: true, message: "产品不能为空",}
        ],
        electronicAntiCompanyConfigurationId: [
          { required: true, message: "公司不能为空",}
        ],
        productionDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        expirationDate: [
          { required: true, message: "失效日期不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, validator: checkAge, trigger: "blur" }
        ]
      },
      title: '',
      open: false,
      loading: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        name: undefined,
      },
      dataList: [],
      companyConfigList: [],
      type: '',
    }
  },
  methods: {
    // 产品弹窗显示
    productShow(){
      this.open = true
      this.title = '产品列表'
      this.type = '1'
      this.getData()
    },
    // 客户弹窗显示
    companyShow(){
      this.open = true
      this.title = '客户列表'
      this.type = '2'
      this.getData()
    },
    //请求产品列表
    getData(){
      this.loading = true
      if(this.type == '1'){
        this.queryParams.delFlag = 0
        antiCodeList(this.addDateRange(this.queryParams)).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false
        })
      }else{
        antiCodeCompanyConfigList(this.addDateRange(this.queryParams)).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false
        })
      }
    },
    // 删除产品
    productClear(){
      this.form.electronicAntiProductId = ''
      this.productName = ''
    },
    // 删除客户
    companyClear(){
      this.form.electronicAntiCompanyConfigurationId = ''
      this.name = ''
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getData()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 选中数据
    onSelectData(row){
      if(this.type == '1'){
        this.productName = row.productName
        this.form.electronicAntiProductId = row.id
        this.open = false
        this.queryParams.productName = undefined
      }else{
        this.name = row.name
        this.form.electronicAntiCompanyConfigurationId = row.id
        this.open = false
        this.queryParams.name = undefined
      }
    },
    // 关闭弹窗
    handleClose(){
      this.open = false
      this.queryParams.productName = undefined
      this.queryParams.delFlag = undefined
      this.queryParams.name = undefined
    },
    onChangeProductionDate(e){
      var date = new Date(e)
      this.productionDate = date.getTime()
      if(this.expirationDate == undefined || this.expirationDate == ''){
        // console.log('111')
      }else{
        if(this.productionDate >= this.expirationDate){
          this.form.productionDate = ''
          this.productionDate = ''
          this.$modal.msgError('生产日期不能大于失效日期！')
        }
      }
    },
    onChangeExpirationDate(e){
      var date = new Date(e)
      this.expirationDate = date.getTime()
      if(this.productionDate > 0){
        if(this.expirationDate <= this.productionDate){
          this.form.expirationDate = ''
          this.expirationDate = ''
          this.$modal.msgError('失效日期必须大于生产日期！')
        }
      }else{
        this.form.expirationDate = ''
        this.expirationDate = ''
        this.$modal.msgError('生产日期不能为空！')
      }
    },
    // 下载子码
    getSubCode(){
      // console.log(this.form)
      this.$refs["form"].validate(v => {
        if(v){
          this.$modal.loading("正在下载")
          antiCodeGenerateCode(this.form).then((res)=>{
            this.$modal.closeLoading()
            this.form.num = ''
            let str = new Blob([res], {type: 'text/plain;charset=utf-8'})
            saveAs(str, `防伪码.txt`)
          })
        }
      })
    }
  }
}
</script>
